<?php
namespace Modules\Home\Controllers;

class UserWalletGoodsController extends ControllerBase {
  public function initialize() {
    //parent::initialize();

    $this -> _checkLogin();
  }

  public function sellAction() {
    $user = $this -> session -> get('user');
    $resValidation = $this -> user::getWalletValidationInstance()
    -> addAmount()
    -> addSafePassword($user['user_id'])
    -> execute($this -> request -> getPost());
    if($resValidation['code'] != 0) { return $this -> _returnScript($resValidation['message'], '/finance'); }

    $resInfo = $this -> user::getInfoServiceInstance() -> findFirst(['conditions' => 'user_id = ' . $user['user_id']]);
    if($resInfo['code'] != 0) { return $this -> _returnScript($resInfo['message'], '/finance'); }

    $resInfo['model'] -> bank_name = trim($resInfo['model'] -> bank_name, ' ');
    $resInfo['model'] -> bank_account = trim($resInfo['model'] -> bank_account, ' ');
    $resInfo['model'] -> bank_account_name = trim($resInfo['model'] -> bank_account_name, ' ');
    $resInfo['model'] -> bank_deposit = trim($resInfo['model'] -> bank_deposit, ' ');
    if(
      is_null($resInfo['model'] -> bank_name) || empty($resInfo['model'] -> bank_name) ||
      is_null($resInfo['model'] -> bank_account) || empty($resInfo['model'] -> bank_account) ||
      is_null($resInfo['model'] -> bank_account_name) || empty($resInfo['model'] -> bank_account_name) ||
      is_null($resInfo['model'] -> bank_deposit) || empty($resInfo['model'] -> bank_deposit)
    ) {
      return $this -> _returnScript('请先完成银行信息', '/user/profile');
    }

    $res = $this -> user::getWalletServiceInstance() -> sell(1, $user['user_id'], $resValidation['data']['amount'], '用户操作');
    return $this -> _returnScript($res['message'], '/finance');
  }

  public function repeatedAction() {
    $user = $this -> session -> get('user');

    $resValidation = $this -> user::getWalletValidationInstance()
    -> addAmount()
    -> addSafePassword($user['user_id'])
    -> execute($this -> request -> getPost());
    if($resValidation['code'] != 0) {
      return $this -> _returnScript($resValidation['message'], '/finance');
    }

    $res = $this -> user::getWalletServiceInstance() -> repeated($user['user_id'], $resValidation['data']['amount'], '用户操作');
    return $this -> _returnScript($res['message'], '/finance');
  }

  public function transferAction() {
    $user = $this -> session -> get('user');
    $resValidation = $this -> user::getWalletValidationInstance()
    -> addSafePassword($user['user_id'])
    -> execute($this -> request -> getPost());
    if($resValidation['code'] != 0) {
      return $this -> _returnScript($resValidation['message'], '/finance');
    }

    $resUser = $this -> user::getServiceInstance() -> findFirst('user_code = "' . $this -> request -> getPost('user_code', 'string', '', true) . '"');
    if($resUser['code'] != 0) {
      return $this -> _returnScript($resUser['message'], '/finance');
    }

    $res = $this -> user::getWalletServiceInstance() -> transfer(1, $user['user_id'], $resUser['model'] -> user_id, $this -> request -> getPost('amount', 'float', 0, true), 0, '用户操作');
    return $this -> _returnScript($res['message'], '/finance');
  }
}